﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CBMS.Interfaces;
using CBMS.Models;
using System.Data.Objects.DataClasses;
using System.Data.Objects; 
using System.Collections;
using CBMS.Objects;
using System.Data.Objects.SqlClient;
using CBMS.Common;
using System.Data.Common;
using System.Data.EntityClient;
using System.Data;
using System.Globalization;

namespace CBMS.Business
{
    public class MemberRepository : BaseEntityRepository<Member>, IMemberRepository
    {
        public MemberRepository()
            : base()
        {

        }
        public Member GetMemberById(int memberId)
        {
            Member objMember = new Member();
            try{
                objMember = List().Where(m => m.member_id == memberId).FirstOrDefault();
            }catch{}
            return objMember;
        }
        public IList<MemberResultDTO> SearchMembersBySomeCondition(MemberCondition memberCondition, int pageIndex, int pageSize, string sortField, string sortExpression, ref int toTal)
        {
            try
            {
                var emp = DbContext.sp_SearchMembers(memberCondition.membership_num, memberCondition.member_type, memberCondition.first_name, memberCondition.last_name, memberCondition.phone, sortField, sortExpression, pageIndex, pageSize);
                toTal = 0;
                var query = (from c in emp
                             select new MemberResultDTO
                             { 
                                 member_id = c.member_id,
                                 membership_num = c.membership_num,
                                 first_name = c.first_name,
                                 last_name = c.last_name,
                                 member_type = c.member_type,
                                 subcategory_name = c.subcategory_name,
                                 email = c.email,
                                 phone = c.phone,
                                 birthday = c.birthday,
                                 address = c.address,
                                 Total = c.TotalRow
                             }).DefaultIfEmpty<MemberResultDTO>();
                var lstResult = query.ToList<MemberResultDTO>();
                if (lstResult == null || lstResult[0] == null)
                {
                    return new List<MemberResultDTO>();
                }
                else
                {
                    toTal = lstResult.FirstOrDefault().Total;
                }
                return lstResult;
            }
            catch
            {
                return new List<MemberResultDTO>();
            }
        }
        public bool CheckDuplicateMembershipNumber(string membershipNumber)
        {
            Member objMember = new Member();
            try
            {
                objMember = List().Where(m => m.membership_num.ToLower().Equals(membershipNumber.ToLower())).FirstOrDefault();
            }
            catch { }
            return (objMember != null);
        }
    }
}